package qingma

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"google.golang.org/grpc/status"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/proto/grpc/burial"
)

type QmBurialApi struct {
}

// CreateQmBurial 创建qmBurial表
// @Tags QmBurial
// @Summary 创建qmBurial表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body qingma.QmBurial true "创建qmBurial表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmBurial/createQmBurial [post]
func (qmBurialApi *QmBurialApi) CreateQmBurial(c *gin.Context) {
	var data burial.Burial
	err := c.ShouldBindJSON(&data)
	data.GameHash = c.Request.Header.Get("game-hash")
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	_, err = grpcClient.Create(context.Background(), &burial.CreateRequest{
		Burial: &data,
	})
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (qmBurialApi *QmBurialApi) UpdateQmBurial(c *gin.Context) {
	var data burial.Burial
	err := c.ShouldBindJSON(&data)
	data.GameHash = c.Request.Header.Get("game-hash")
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	_, err = grpcClient.Update(context.Background(), &burial.UpdateRequest{
		Burial: &data,
	})
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

func (qmBurialApi *QmBurialApi) GetQmBurialList(c *gin.Context) {
	var req burial.ListRequest
	req.GameHash = c.Request.Header.Get("game-hash")
	req.Page = cast.ToInt64(c.Query("page"))
	req.PageSize = cast.ToInt64(c.Query("pageSize"))
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	burials, err := grpcClient.List(context.Background(), &req)
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithData(response.PageResult{
		List:     burials.Burial,
		Total:    burials.Total,
		Page:     int(req.Page),
		PageSize: int(req.PageSize),
	}, c)
}

func (qmBurialApi *QmBurialApi) DeleteQmBurialById(c *gin.Context) {
	ID := c.Query("ID")
	//转成int64
	id := cast.ToInt64(ID)
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	_, err := grpcClient.Delete(context.Background(), &burial.DeleteRequest{Id: id})
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

func (qmBurialApi *QmBurialApi) GetQmBurialById(c *gin.Context) {
	ID := c.Query("ID")
	var req burial.GetRequest
	req.Id = cast.ToInt64(ID)
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	burial, err := grpcClient.Get(context.Background(), &req)
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithData(burial, c)
}

func (qmBurialApi *QmBurialApi) GetQmBurialDataList(c *gin.Context) {
	var req burial.GetDataRequest
	type request struct {
		StartDate string `form:"start_date"`
		EndDate   string `form:"end_date"`
		Flag      string `form:"flag"`
		Type      int64  `form:"type"`
	}
	var queryReq request
	c.ShouldBindQuery(&queryReq)
	req.GameHash = c.Request.Header.Get("game-hash")
	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))
	req.StartDate = queryReq.StartDate
	req.EndDate = queryReq.EndDate
	req.Flag = queryReq.Flag
	req.Type = queryReq.Type
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	burialData, err := grpcClient.GetData(context.Background(), &req)
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithData(&burialData, c)
}

func (qmBurialApi *QmBurialApi) GetQmEventDetailList(c *gin.Context) {
	var req burial.GetEventDetailRequest
	type request struct {
		StartDate string `form:"start_date"`
		EndDate   string `form:"end_date"`
		Flags     string `form:"flags"`
		OpenId    string `form:"openid"`
		Page      int64  `form:"page"`
		PageSize  int64  `form:"page_size"`
	}
	var queryReq request
	c.ShouldBindQuery(&queryReq)
	req.GameHash = c.Request.Header.Get("game-hash")
	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))
	req.StartDate = queryReq.StartDate
	req.EndDate = queryReq.EndDate
	req.Flags = queryReq.Flags
	req.Openid = queryReq.OpenId
	req.Page = queryReq.Page
	req.PageSize = queryReq.PageSize
	grpcClient := burial.NewBurialServiceClient(global.OperateGrpc)
	eventDetail, err := grpcClient.GetEventDetail(context.Background(), &req)
	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(&eventDetail, c)
}
